import * as NSBase from "./NetServiceBase";
import { Config } from "../Config";
import {
    V_Store,
    T_ReceivingAddress,
    V_ReceivingAddress,
    V_Category,
    V_Goods,
    V_Cart,
    V_Currency,
    V_Member_Coupon,
    V_Order
} from "./Entitys";

console.log("entservices...");

//查询店铺首页数据
export var QueryStoreIndex = (): Promise<V_Store> => {
    return new Promise<V_Store>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryStoreIndex);
            console.log(r);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};
//查询分类
export var QueryCategory = (): Promise<Array<V_Category>> => {
    return new Promise<Array<V_Category>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryCategory);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};
//按分类查询商品
export var QueryAllGoods = (CategoryId: number, SortType: number): Promise<Array<V_Goods>> => {
    return new Promise<Array<V_Goods>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryAllGoods, {
                CategoryId: CategoryId,
                SortType: SortType
            });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};
//今日特卖
export var QuerySaleToDay = (): Promise<Array<V_Goods>> => {
    return new Promise<Array<V_Goods>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QuerySaleToDay);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};
//查询商品详情QueryGoodsDetails
export var QueryGoodsDetails = (GoodsNo: number): Promise<V_Goods> => {
    return new Promise<V_Goods>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryGoodsDetails, { GoodsNo: GoodsNo });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//查询购物车列表
export var QueryUserCart = (): Promise<Array<V_Cart>> => {
    return new Promise<Array<V_Cart>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryUserCart);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//查询确认订单的购物车列表
export var QuerySelectedUserCart = (): Promise<Array<V_Cart>> => {
    return new Promise<Array<V_Cart>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QuerySelectedUserCart);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//修改购物车选中状态
export var UpdateCartSelectState = (CartId: number, State: boolean): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            await NSBase.WX_request(Config.UpdateCartSelectState, { CartId, State });
            resolve();
        } catch (e) {
            reject(e);
        }
    });
};

//修改全部可购商品全选全不选
export var UpdateCartSelectAll = (CartIdArr: number[], State: boolean): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            await NSBase.WX_request(
                Config.UpdateCartSelectAll + "?" + ObjparseParam({ CartIdArr, State })
            );
            resolve();
        } catch (e) {
            reject(e);
        }
    });
};

//修改商品数量
export var UpdateCartCount = (CartId: number, Count: number): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            await NSBase.WX_request(Config.UpdateCartCount, { CartId, Count });
            resolve();
        } catch (e) {
            reject(e);
        }
    });
};

//购物车
export var AddToCart = (GoodsNo: string, Count: number, SkuId: number): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.AddToCart, {
                GoodsNo: GoodsNo,
                Count: Count,
                SkuId: SkuId
            });
            console.log(r);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//DeleteCart 删除购物车商品
export var DeleteCart = (CartId: number): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.DeleteCart, { CartId: CartId });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//DeleteCartList 根据ID列表批量删除购物车商品
export var DeleteCartList = (CartIdArr: number[]): Promise<void> => {
    return new Promise<void>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(
                Config.DeleteCartList + "?" + ObjparseParam({ CartIdArr })
            );
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//我的太云币
export var QueryMemberTyCurrency = (IsEffective: boolean): Promise<Array<V_Currency>> => {
    return new Promise<Array<V_Currency>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryMemberTyCurrency, {
                IsEffective: IsEffective
            });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//我的优惠券
export var QueryMemberCoupon = (IsEffective: boolean): Promise<Array<V_Member_Coupon>> => {
    return new Promise<Array<V_Member_Coupon>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryMemberCoupon, { IsEffective: IsEffective });
            console.log(r);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//查询用户地址
export var QueryMemberAddress = (): Promise<Array<V_ReceivingAddress>> => {
    return new Promise<Array<V_ReceivingAddress>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryMemberAddress);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//添加用户地址
export var AddMemberAddress = (
    ReceivingAddress: T_ReceivingAddress
): Promise<T_ReceivingAddress> => {
    return new Promise<T_ReceivingAddress>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.AddMemberAddress, ReceivingAddress);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//修改用户地址
export var EditUserAddress = (
    ReceivingAddress: T_ReceivingAddress
): Promise<T_ReceivingAddress> => {
    return new Promise<T_ReceivingAddress>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.EditUserAddress, ReceivingAddress);
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//我的订单
export var QueryMemberOrderList = (
    Status: number,
    PageIndex: number,
    PageSize: number
): Promise<Array<V_Order>> => {
    return new Promise<Array<V_Order>>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryMemberOrderList, {
                Status: Status,
                PageIndex: PageIndex,
                PageSize: PageSize
            });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//我的订单详情
export var QueryOrderDetails = (OrderId: number): Promise<V_Order> => {
    return new Promise<V_Order>(async (resolve, reject) => {
        try {
            let r = await NSBase.WX_request(Config.QueryOrderDetails, { OrderId: OrderId });
            resolve(r);
        } catch (e) {
            reject(e);
        }
    });
};

//转换为可以接收List<T>的格式
export var ObjparseParam = (data: any) => {
    if (typeof data != "object") {
        return "";
    }
    var urlEncode = (param: any, key?: any, encode?: any) => {
        if (param == null) return "";
        var paramStr = "";
        var t = typeof param;
        if (t == "string" || t == "number" || t == "boolean") {
            paramStr +=
                "&" + key + "=" + (encode == null || encode ? encodeURIComponent(param) : param);
        } else {
            for (var i in param) {
                var k =
                    key == null
                        ? i
                        : key + (param instanceof Array ? "[" + i + "]" : "[" + i + "]");
                paramStr += urlEncode(param[i], k, encode);
            }
        }
        return paramStr;
    };
    return urlEncode(data).substring(1);
};
